package com.bianguo.android.functiondemo.view.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.ViewDragHelper;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.utils.GlideUtil;
import com.bianguo.android.functiondemo.view.widget.MNGestureView;
import com.bianguo.android.functiondemo.view.widget.MNViewPager;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;

import butterknife.BindView;

public class ImageActivity extends BaseActivity implements ViewPager.OnPageChangeListener, MNGestureView.OnCanSwipeListener, MNGestureView.OnSwipeListener {

    @BindView(R.id.image_bg_layout)
    RelativeLayout bgLayout;
    @BindView(R.id.image_pager)
    MNViewPager imagePager;
    @BindView(R.id.image_gesture_view)
    MNGestureView gestureView;

    private ArrayList<String> imageUrls;
    private ArrayList<PhotoView> views;
    private PhotoView currentView;
    private ViewDragHelper mDragHelper;

    @Override
    public int layoutId() {
        return R.layout.activity_image;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        Intent intent = getIntent();
        imageUrls = intent.getStringArrayListExtra("imageUrls");
        views = new ArrayList<>();
        int size = imageUrls.size();
        for (int i = 0; i < size; i++) {
            PhotoView view = new PhotoView(mContext);
            view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            GlideUtil.loadImage(mContext, imageUrls.get(i), view, -1);
            view.setOnClickListener(v -> ImageActivity.this.finish());
            views.add(view);
        }
        imagePager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imageUrls != null ? imageUrls.size() : 0;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                PhotoView photoView = views.get(position);
                container.addView(photoView);
                return photoView;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });
        imagePager.addOnPageChangeListener(this);
        currentView = views.get(0);
        gestureView.setOnGestureListener(this);
        gestureView.setOnSwipeListener(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        currentView = views.get(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onActivityReenter(int resultCode, Intent data) {
        super.onActivityReenter(resultCode, data);
    }

    @Override
    public boolean canSwipe() {
        if (currentView.getScale() != 1.0) {
            return false;
        }
        return true;
    }

    @Override
    public void downSwipe() {
        finishBrowser();
    }

    @Override
    public void overSwipe() {
        bgLayout.setAlpha(1);
    }

    @Override
    public void onSwiping(float deltaY) {
        float mAlpha = 1 - deltaY / 500;
        if (mAlpha < 0.3) {
            mAlpha = 0.3f;
        }
        if (mAlpha > 1) {
            mAlpha = 1;
        }
        bgLayout.setAlpha(mAlpha);
    }

    private void finishBrowser() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        finish();
        this.overridePendingTransition(0, R.anim.mn_browser_exit_anim);
    }
}
